package com.nd.android.u.ui.activity.chat_config;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Build;

import com.common.android.utils.ToastUtils;
import com.common.android.utils.task.genericTask.GenericTask;
import com.common.android.utils.task.genericTask.TaskAdapter;
import com.common.android.utils.task.genericTask.TaskListener;
import com.common.android.utils.task.genericTask.TaskParams;
import com.common.android.utils.task.genericTask.TaskResult;
import com.nd.android.u.controller.innerInterface.IMessageDisplay;
import com.nd.android.u.ui.R;
import com.nd.android.u.ui.chatUiUtils.AudioQuenePlayManager;

/**
 * 删除聊天记录对话框
 * <br>Created 2014-7-25 下午5:45:50
 * @version  
 * @author   cb		
 * @see 	 
 */
public class DeleteHistoryDialog  extends AlertDialog.Builder{
	private Context mContext;
	private GenericTask mDeleteTask;
	private ProgressDialog mDialog;
	private IMessageDisplay mMessage;
	/**
	 * Creates a new instance of DeleteHistoryDialog.
	 * <br>Created 2014-7-25 下午5:45:48
	 * @param context
	 * @param message
	 */
	public DeleteHistoryDialog(Context context,IMessageDisplay message) {
		super(context);
		this.mContext = context;
		mMessage=message;
		this.setTitle(R.string.chat_clear_history);
		this.setMessage(R.string.chat_confirm_clear);
		initEvent();
	}
	
	
	/**
	 * 初始化事件
	 * <br>Created 2014-7-25 下午5:45:36
	 * @author       cb
	*/
	private void initEvent(){
		this.setPositiveButton(R.string.ok, new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				AudioQuenePlayManager.getInstance().stopPlayAndSetCureentNull();
				deleteChatRecord();
				dialog.dismiss();
			}
		});
		this.setNegativeButton	(R.string.cancel,new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
	}
	
	/**
	 * 删除聊天记录任务
	 * @author Administrator
	 */
	private class DeleteChatRecordTask extends GenericTask {		
		@Override
		protected TaskResult _doInBackground(TaskParams... params) {
			mMessage.getProccessInterface().deleteAllMessage();
			return TaskResult.OK;
		}
	}
	
	/**
	 * 删除聊天记录
	 * <br>Created 2014-7-25 下午5:46:13
	 * @author       cb
	*/
	@TargetApi(Build.VERSION_CODES.CUPCAKE)
	private void deleteChatRecord() {
		if (mDeleteTask != null && mDeleteTask.getStatus() == GenericTask.Status.RUNNING) {
			return;
		} else {
			mDeleteTask = new DeleteChatRecordTask();
			mDeleteTask.setListener(deleteChatRecordTaskListener);
			TaskParams params = new TaskParams();
			mDeleteTask.execute(params);
		}
	}

	//监听
	private TaskListener deleteChatRecordTaskListener = new TaskAdapter() {		
		@Override
		public void onPreExecute(GenericTask task) {
			mDialog = ProgressDialog.show(mContext, "", mContext.getString(R.string.chat_clearing_history),true);
		}

		@TargetApi(Build.VERSION_CODES.CUPCAKE)
		@Override
		public void onPostExecute(GenericTask task, TaskResult result) {
			if(mDialog!=null){
				mDialog.dismiss();
			}
			if (mDeleteTask != null && mDeleteTask.getStatus() == GenericTask.Status.RUNNING) {
				mDeleteTask.cancel(true);
			}
			if (result==TaskResult.FAILED){
				ToastUtils.display(R.string.delete_fail);
			}
		}
	};
}